<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px">
            <el-form-item label="父级菜单" prop="pid">
                <el-cascader v-model="ruleForm.pid" :options="ids" :props="cascaderProps" :show-all-levels="false"></el-cascader>
            </el-form-item>
            <el-form-item label="功能名称" prop="title">
                <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="api" prop="resources">
                <el-input v-model="ruleForm.resources" placeholder="多个api用;连接"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="sub('ruleForm')">确定</el-button>
                <el-button @click="onCancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { apiGetMenuList, apiMenuAdd } from "@/api/admin.js";
export default {
    name: "menu_index_add_resources",
    props: {
        ids: {
            type: Array,
            require: true
        }
    },
    data() {
        return {
            cascaderProps: {
                label: 'name', value: 'id',
                lazy: true,
                lazyLoad(node, resolve) {
                    // console.warn('node', node);
                    if (node.data) {
                        apiGetMenuList({ pid: node.data.id }).then(({ data }) => {
                            const nodes = data.map(item => ({ id: item.id, name: item.title, leaf: node.level < 2 }))
                            resolve(nodes); // 通过调用resolve将子节点数据返回，通知组件数据加载完成
                        });
                    }
                }
            },
            ruleForm: {
                pid: "", //父级菜单id
                title: "",
                resources: "",
                type: "PLATFORM",
                level: "2",
            },
            rules: {
                pid: [{ required: true, message: "请选择父级菜单", trigger: "change" }],
                title: [{ required: true, message: "请输入功能名称", trigger: "change" }],
                resources: [{ required: true, message: "请输入api", trigger: "change" }],
            },
            loading: false,
        }
    },
    methods: {
        // 确定
        sub(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    const postData = JSON.parse(JSON.stringify(this.ruleForm))
                    postData.pid = postData.pid[1]
                    postData.resources = postData.resources.split(';')
                    apiMenuAdd(postData).then((res) => {
                        this.$message({ message: "操作成功", type: "success" });
                        this.loading = false;
                        this.$emit("onSubmit");
                    }).catch((err) => {
                        this.loading = false;
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        // 取消
        onCancel() {
            this.loading = false;
            this.$emit("onCancel");
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
}
</script>

<style></style>